﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using BusinessLayer.FindArtInfo.Entities;
using BusinessLayer.RegInfo.Entities;
using DBHelper;

namespace BusinessLayer.RegInfo
{
    public class AdvertiseBL
    {
        public Advertise GetCurrent()
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            var list = adapter.GetList<Advertise>("sp_Advertise_Select_Current", CommandType.StoredProcedure,
                                                   CustomConnectionState.CloseOnExit);
            return list.FirstOrDefault();
        }

        public List<Advertise> GetAll(int pageIndex, int pageSize, out int totalRecord)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@PageIndex", pageIndex);
            adapter.AddParameter("@PageSize", pageSize);
            var output = adapter.AddParameter("@TotalRecord", SqlDbType.Int, ParameterDirection.Output);
            var list = adapter.GetList<Advertise>("sp_Advertise_Select_All", CommandType.StoredProcedure,
                                                   CustomConnectionState.CloseOnExit);
            totalRecord = Convert.ToInt32(output.Value);
            return list;
        }

        public Advertise GetById(int id)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@Id", id);
            var list = adapter.GetList<Advertise>("sp_Advertise_Select_By_Id", CommandType.StoredProcedure,
                                                   CustomConnectionState.CloseOnExit);
            return list.FirstOrDefault();
        }

        public void Insert(Advertise advance)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@Title", advance.Title);
            adapter.AddParameter("@ImageName", advance.ImageName);
            adapter.AddParameter("@StartDate", advance.StartDate);
            adapter.AddParameter("@EndDate", advance.EndDate);
            adapter.AddParameter("@UrlRedirect", advance.UrlRedirect);
            adapter.ExecuteNonQuery("sp_Advertise_Insert", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
        }

        public void Update(Advertise advance)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@Id", advance.Id);
            adapter.AddParameter("@Title", advance.Title);
            adapter.AddParameter("@ImageName", advance.ImageName);
            adapter.AddParameter("@StartDate", advance.StartDate);
            adapter.AddParameter("@EndDate", advance.EndDate);
            adapter.AddParameter("@UrlRedirect", advance.UrlRedirect);
            adapter.ExecuteNonQuery("sp_Advertise_Update", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
        }

        public void Delete(int id)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@Id", id);
            adapter.ExecuteNonQuery("sp_Advertise_Delete", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
        }
    }
}
